﻿namespace EyeOpen.Data
{
	using System.Collections.Generic;
	using System.Linq;
	using EyeOpen.Domain;

	public class WebSiteDbPopulator
		: DbPopulator<WebSiteDbContext>
	{
		private const string TwoLetter = "en";
		
		private const string Home = "Home";
		private const string Credits = "Credits";
		private const string Project = "Project";

		public override void Populate(WebSiteDbContext dbContext)
		{
			var languageList = CreateLanguage(new EntityBuilder<Language>());

			var contentList = CreateContent(new EntityBuilder<Content>());

			AddToRepository(dbContext.ContentLocalizedRepository, CreateContentLocalized(languageList, contentList));

			AddToRepository(dbContext.UserRepository, CreateUser(new EntityBuilder<User>()));
		}

		private IEnumerable<User> CreateUser(EntityBuilder<User> entityBuilder)
		{
			yield return entityBuilder.Create(x => { x.Email = "admin@easycms.eye"; x.Password = User.CreatePassword("admin"); x.Locked = false; });
		}

		private IEnumerable<Content> CreateContent(EntityBuilder<Content> entityBuilder)
		{
			yield return entityBuilder.Create(x => { x.Name = Home; });
			yield return entityBuilder.Create(x => { x.Name = Credits; });
			yield return entityBuilder.Create(x => { x.Name = Project; });
		}

		private IEnumerable<Language> CreateLanguage(EntityBuilder<Language> entity)
		{
			yield return entity.Create(x => { x.TwoLetter = TwoLetter; x.Name = "English"; });
		}

		private IEnumerable<ContentLocalized> CreateContentLocalized(IEnumerable<Language> languageList, IEnumerable<Content> contentList)
		{
			var home = contentList.Single(x => x.Name == Home);
			var credits = contentList.Single(x => x.Name == Credits);
			var project = contentList.Single(x => x.Name == Project);

			var language = languageList.Single(x => x.TwoLetter == TwoLetter);

			yield return
				CreateContentLocalized(
					"Home", 
					"<div>To learn more about ASP.NET MVC Easy CMS visit <a href=\"http://sample.codeplex.com\" title=\"ASP.NET MVC Easy CMS Website\">http://sample.codeplex.com</a>.</div>", 
					0, 
					home, 
					language);

			yield return
				CreateContentLocalized(
					"Credits",
					"<div>Here the project coordinator <a href=\"http://www.codeplex.com/site/users/view/teoarch\">Matteo Migliore</a>.</div>",
					1,
					credits,
					language);

			yield return
				CreateContentLocalized(
					"Project",
					"<div>This CMS is a sample of combination of ASP.NET MVC and EntityFramework.</div>",
					2,
					project,
					language);
		}

		private ContentLocalized CreateContentLocalized(string text, string body, int order, Content content, Language language)
		{
			const string Title = "EyeOpen - Easy MVC CMS";
			const string Description = "Description here";
			const string Keywords = "CMS,ASP.NET,MVC";

			return
				new EntityBuilder<ContentLocalized>()
					.Create(
						x =>
						{
							x.Text = text;
							x.Path = text == "Home" ? null : text.ToLower();
							x.Title = string.Format("{0} - {1}", Title, x.Text);
							x.Description = Description;
							x.Keywords = Keywords;
							x.Body = body;
							x.Order = order;
							x.Language = language;
							x.Content = content;
						});
		}
	}
}